package com.hamster.crmpre.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hamster.crmpre.mapper.ChatRecordMapper;
import com.hamster.crmpre.pojo.ChatRecord;
import com.hamster.crmpre.pojo.dto.ImageList;
import com.hamster.crmpre.pojo.dto.ImageUrl;
import com.hamster.crmpre.service.ChatRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 聊天记录的Service
 * @author Tarry
 * @create 2020/2/13 13:47
 */
@Service
public class ChatRecordServiceImpl implements ChatRecordService {
    @Autowired
    private ChatRecordMapper chatRecordMapper;
    /**
     * 提交保存聊天记录
     * @param customerId    客户id
     * @param employeeId    员工id，当前录入员工
     * @param urlList       聊天记录截图的url
     */
    @Override
    @Transactional
    public void saveRecord(String customerId, String employeeId, List<String> urlList) {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        Date createTime = todayStart.getTime();
        for (String url : urlList) {
            ChatRecord chatRecord = new ChatRecord();
            chatRecord.setCustomerId(customerId);//客户id
            chatRecord.setEmployeeId(employeeId);//员工id
            chatRecord.setDel("0");//删除标记 正常
            chatRecord.setImageUrl(url);//截图的url
            chatRecord.setCreateTime(createTime);//创建时间
            chatRecord.setInputTime(new Date());//插入时间
            chatRecordMapper.insert(chatRecord);
        }
    }

    /**
     * 查询该客户的聊天记录截图
     * @param customerId 客户id
     * @param employeeId 员工id
     * @param soft       排序  asc 升序   desc 降序
     * @return
     */
    @Override
    public List<ImageList> listRecord(String customerId, String employeeId, String soft) {
//        QueryWrapper<ChatRecord> chatRecordQueryWrapper = new QueryWrapper<>();
//        chatRecordQueryWrapper.eq("del","0").eq("customer_id",customerId).eq("employee_id",employeeId);
//        if (StringUtils.isBlank(soft) || "asc".equals(soft)){//升序
//            chatRecordQueryWrapper.orderByAsc("create_time");
//        }else if("desc".equals(soft)){//降序
//            chatRecordQueryWrapper.orderByDesc("create_time");
//        }
//        List<ChatRecord> recordList = chatRecordMapper.selectList(chatRecordQueryWrapper);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd#HH:mm:ss");
        List<ImageUrl> imageUrlList = chatRecordMapper.listRecord(customerId, employeeId);
        List<ImageList> list = new ArrayList<>();
        String curDate = "";//临时变量
        List<ImageUrl> curImageUrlList = new ArrayList<>();//临时变量
        for (ImageUrl imageUrl : imageUrlList) {
            String format = simpleDateFormat.format(imageUrl.getCreateTime());
            String dateStr = format.split("#")[0];
            if (!dateStr.equals(curDate)){
                if (curImageUrlList.size() > 0){
                    ImageList imageList = new ImageList();
                    imageList.setFileList(curImageUrlList);
                    imageList.setCreateTime(curImageUrlList.get(0).getCreateTime());
                    list.add(imageList);
                }
                curImageUrlList = new ArrayList<>();
            }
            curImageUrlList.add(imageUrl);

            curDate = dateStr;
        }
        if (curImageUrlList.size() > 0){
                ImageList imageList = new ImageList();
                imageList.setFileList(curImageUrlList);
                imageList.setCreateTime(curImageUrlList.get(0).getCreateTime());
                list.add(imageList);
        }

        return list;
    }

    /**
     * 删除该客户的 聊天记录截图
     * @param id  该截图的id
     */
    @Override
    public void deleteRecord(String id) {
        chatRecordMapper.deleteRecord(id);
    }
}
